<?php
class Mobile extends AppModel {
	var $name = "Job";
	var $useTable = "job_mst";
	var $primaryKey = 'id';
	
	protected $images_options = array('Att_3'=>array('sort'=>1,'img'=>'/images/state/zero.gif','css'=>'icLang')
									  ,'Mem_1'=>array('sort'=>2,'img'=>'/images/state/newman.gif','css'=>'icLang'),
									  'Mem_3'=>array('sort'=>3,'img'=>'/images/state/s.gif','css'=>'icLang'),
										'Mem_2'=>array('sort'=>4,'img'=>'/images/state/bg.gif','css'=>'icLang'),
									  'Att_1'=>array('sort'=>5,'img'=>'/images/state/new_g.gif','css'=>'icLang')
										,'Att_2'=>array('sort'=>6,'img'=>'/images/state/new_b.gif','css'=>'icLang'),
									  'Lan_4'=>array('sort'=>7,'img'=>'/images/state/en.gif','css'=>'icLang')
								);
	public $category_options = array(101,102,103,104,105,106,107,115,108,109,110,111,112,162,113);
	/**
	 * job search list 
	 */
	function getJobList($tameid = null,$areaid=null,$attid=null,$hmid=null,$page = 1,$temas,$lang_data,$memb_type,$attention_data,$pagesize=20)
	{
		$jobList = array();
		//First is  attention Information
		if($page == 1)
		{
			$jobList = $this->getAttentionJobList($tameid,$areaid,$attid,$hmid,1,$temas,$lang_data,$memb_type,$attention_data,1);
			if(isset($_GET['test']) && $_GET['test']=='test')
			{
				header("Content-type: text/html; charset=utf-8");
				print_r($jobList);exit;
			}
		}
		$date = date('Y-m-d');
		$datetime = date('Y-m-d H:i:s');
		$fileds = " Job.title,Job.company_id,Job.work_type,Job.content,Job.id as job_id,Job.pickup_img_file,Job.attention_img_file,Job.img_file,Job.small_title,(Job.public_start||' '||Job.public_start_hour||':'||Job.public_start_min) as pustart";
		$select = "select ".$fileds." from job_mst as   Job  join company_info as  cinfo on (cinfo.id=Job.company_id and cinfo.deleted is null)";
		$where =" WHERE	Job.deleted IS NULL AND Job.search_flg = 0 AND (Job.public_close_date IS NULL) AND (Job.cn_asia <> '1' OR Job.cn_asia IS NULL)";
// 		$where .=" AND (((  Job.attention_img_file != '' AND Job.attention_search_flg = 1 and Job.attention_start <= '".$date."' AND Job.attention_end >= '".$date."')";
// 		$where .=" or (Job.pickup_img_file!='' and Job.pickup_search_flg = 1 and Job.pickup_start<='".$date."' and Job.pickup_end>='".$date."')";
// 		$where .=" or (Job.public_start is not null and Job.attention_start is null and Job.pickup_start is null)) AND 	(Job.public_start || ' ' || Job.public_start_hour || ':' || Job.public_start_min<= '".$datetime."'";
		$where .=" and (Job.public_start || ' ' || Job.public_start_hour || ':' || Job.public_start_min<= '".$datetime."'";
		$where .=" AND 	Job.public_end || ' ' || Job.public_end_hour || ':' || Job.public_end_min>= '".$datetime."')";
		if(isset($jobList[0]))
			$where .=" and Job.id!=".$jobList[0]['job_id'];

		$groupby = '';
		if($tameid)
		{
			$join = " join job_category_inner as cinner on (cinner.job_id = Job.id and cinner.deleted is null)";
			$where .=" and category_id=".$tameid;
		}
		if($areaid)
		{
			$join .=" join job_area_inner as ainner on (ainner.job_id = Job.id and ainner.deleted is null) ";
			if($areaid == 9397){
				$where .=" and job_area_id in (93,94,96,97)";
				$groupby = ' group by Job.id,company_id,Job.title,Job.work_type,Job.content,job.pickup_img_file,job.attention_img_file,job.img_file,Job.small_title,pustart,job.approve_date';
				
			}else
				$where .=" and job_area_id=".$areaid;
			
		}
		if($attid)
		{
			$join =" join job_attention_inner as jainner on (jainner.job_id = Job.id and jainner.deleted is null) ";
			$where .=" and attention_id=".$attid;
		}
		if($hmid)
		{
			$join =" join job_hope_member_inner as hminner on (hminner.job_id = Job.id and hminner.deleted is null) ";
			$where .=" and hope_member_id=".$hmid;
		}
		$where .=" and Job.company_id not in (select id from company_info as cinfo where cinfo.deleted is not null) ";
		$order = " order by pustart desc ,Job.approve_date desc";
		$selectcount = "select count(*) as count from job_mst as   Job  join company_info as  cinfo on (cinfo.id=Job.company_id and cinfo.deleted is null)";
		
		$sqlcount = $selectcount.$join.$where;

		$datacount = $this->query($sqlcount);
		
		$totalnum = isset($datacount[0][0]['count'])?$datacount[0][0]['count']:0;
		if($totalnum>0)
		{
			if(count($jobList)>0)
				$totalnum = $totalnum+1;
			$totalpages = ceil($totalnum/$pagesize);
			$page = $page<1?1:$page;
			$page = $page>$totalpages?$totalpages:$page;
			$s = ($page-1)*$pagesize;
			$e = $page*$pagesize;
			$e = $e>$totalnum?$totalnum:$e;
			$ss = $s>0?$s:1;

			if($page == 1 && count($jobList)>0)
			{
				$pagesize = 19;
			}else if($page>1)
			{
				$s = $s -1;
			}
			$limit = ' offset '.$s.' limit '.$pagesize;
		}
		$sql = $select.$join.$where.$groupby.$order.$limit;
		$data = $this->query($sql);

		foreach($data as $k=>$v)
		{
			$job = $v[0];
			if($job['attention_img_file'])
				$job['job_img_file'] = '/upload/image/job/'.floor($job['job_id']/1000).'/'.$job['job_id'].'/'.$job['attention_img_file'];
			else if($job['pickup_img_file'])
				$job['job_img_file'] = '/upload/image/job/'.floor($job['job_id']/1000).'/'.$job['job_id'].'/'.$job['pickup_img_file'];
			else if($job['img_file'])
				$job['job_img_file'] = '/upload/image/job/'.floor($job['job_id']/1000).'/'.$job['job_id'].'/'.$job['img_file'];
			$job['Lan'] = $this->getSelectByJobId($job['job_id'],'hope_language_id','job_hope_lang_inner', $lang_data);
			$job['Mem'] = $this->getSelectByJobId($job['job_id'],'hope_member_id','job_hope_member_inner', $memb_type);
			$job['Att'] = $this->getSelectByJobId($job['job_id'],'attention_id','job_attention_inner', $attention_data);
			$job['Cname'] = $this->getSelectByJobId($job['job_id'],'category_id','job_category_inner', $temas);
			$job['company_name'] = $this->getCompanyNameById($job['company_id']);
			$characters = array();
			foreach($job['Mem'] as $k=>$v)
			{
				if(isset($this->images_options['Mem_'.$k]))
				{
					$op = $this->images_options['Mem_'.$k];
					$characters[$op['sort']] = array('img'=>$op['img'],'css'=>$op['css'],'name'=>$v);
				}
			}
			
			foreach($job['Att'] as $k=>$v)
			{
				if(isset($this->images_options['Att_'.$k]))
				{
					$op = $this->images_options['Att_'.$k];
					$characters[$op['sort']] = array('img'=>$op['img'],'css'=>$op['css'],'name'=>$v);
				}
			}
			
			foreach($job['Lan'] as $k=>$v)
			{
				if(isset($this->images_options['Lan_'.$k]))
				{
					$op = $this->images_options['Lan_'.$k];
					$characters[$op['sort']] = array('img'=>$op['img'],'css'=>$op['css'],'name'=>$v);
				}
			}
			ksort($characters) ;
			$job['characters'] =array_slice($characters,0,4);
			$jobList[] = $job;
		}

		//分页
		$curpage = 1;$nextpage = $totalpages;$npages = $page+2;
		if($page!=$totalpages)
		{
			$nextpage = $page+1;
		}
		
		if($page>1){
			$curpage = $page-1;
		}
		if($page+5>$totalpages){
			$npages = $totalpages;
		}else if($page<5 && $totalpages>=5)
		{
			$npages = 5;
		}else if($totalpages<5)
		{
			$npages = $totalpages;
		}
		$urlarr = array('tameid'=>$tameid,'areaid'=>$areaid,'attid'=>$attid,'hmid'=>$hmid);
		$url = http_build_query($urlarr);
		$result = array('data'=>$jobList,'totalpage'=>$totalpages,'curpage'=>$curpage,'nextpage'=>$nextpage,
						'totalnum'=>$totalnum,'npages'=>$npages,'s'=>$ss,'e'=>$e,'url'=>$url);
		return $result;
	}
	
	function getAttentionJobList($tameid = null,$areaid=null,$attid=null,$hmid=null,$page = 1,$temas,$lang_data,$memb_type,$attention_data,$pagesize=20)
	{
		
		$date = date('Y-m-d');
		$datetime = date('Y-m-d H:i:s');
		$fileds = " Job.title,cinfo.company_name as company_name,Job.work_type,Job.content,Job.id as job_id,Job.pickup_img_file,Job.attention_img_file,Job.img_file,Job.small_title,(Job.public_start||' '||Job.public_start_hour||':'||Job.public_start_min) as pustart";
		$select = "select ".$fileds." from job_mst as   Job  join company_info as  cinfo on (cinfo.id=Job.company_id and cinfo.deleted is null)";
		$where =" WHERE	Job.deleted IS NULL AND Job.search_flg = 0 AND job_status_id=1 and (Job.public_close_date IS NULL) AND (Job.cn_asia <> '1' OR Job.cn_asia IS NULL)";
		$where .=" AND (((  Job.attention_img_file != '' AND Job.attention_search_flg = 1 and Job.attention_start <= '".$date."' AND Job.attention_end >= '".$date."')";
		// 		$where .=" or (Job.pickup_img_file!='' and Job.pickup_search_flg = 1 and Job.pickup_start<='".$date."' and Job.pickup_end>='".$date."')";
		// 		$where .=" or (Job.public_start is not null and Job.attention_start is null and Job.pickup_start is null)) AND 	(Job.public_start || ' ' || Job.public_start_hour || ':' || Job.public_start_min<= '".$datetime."'";
		
		$where .=" AND 	Job.public_end || ' ' || Job.public_end_hour || ':' || Job.public_end_min>= '".$datetime."'))";
		$groupby = '';
		if($tameid)
		{
			$join = " join job_category_inner as cinner on (cinner.job_id = Job.id and cinner.deleted is null)";
			$where .=" and category_id=".$tameid;
		}
		if($areaid)
		{
			$join .=" join job_area_inner as ainner on (ainner.job_id = Job.id and ainner.deleted is null) ";
			if($areaid == 9397){
				$where .=" and job_area_id in (93,94,96,97)";
				$groupby = ' group by Job.id,company_name,Job.title,Job.work_type,Job.content,job.pickup_img_file,job.attention_img_file,job.img_file,Job.small_title,pustart,job.approve_date,job.attention_start';
			}else
				$where .=" and job_area_id=".$areaid;
			
		}
		if($attid)
		{
			$join =" join job_attention_inner as jainner on (jainner.job_id = Job.id and jainner.deleted is null) ";
			$where .=" and attention_id=".$attid;
		}
		if($hmid)
		{
			$join =" join job_hope_member_inner as hminner on (hminner.job_id = Job.id and hminner.deleted is null) ";
			$where .=" and hope_member_id=".$hmid;
		}

		$order = " order by attention_start is not null desc,attention_start desc , pustart desc,Job.approve_date desc";
		$limit = ' offset 0 limit '.$pagesize;
		$sql = $select.$join.$where.$groupby.$order.$limit;

		$data = $this->query($sql);
		$jobList = array();
		foreach($data as $k=>$v)
		{
			$job = $v[0];
			if($job['attention_img_file'])
				$job['job_img_file'] = '/upload/image/job/'.floor($job['job_id']/1000).'/'.$job['job_id'].'/'.$job['attention_img_file'];
			else if($job['pickup_img_file'])
				$job['job_img_file'] = '/upload/image/job/'.floor($job['job_id']/1000).'/'.$job['job_id'].'/'.$job['pickup_img_file'];
			else if($job['img_file'])
				$job['job_img_file'] = '/upload/image/job/'.floor($job['job_id']/1000).'/'.$job['job_id'].'/'.$job['img_file'];
			$job['Lan'] = $this->getSelectByJobId($job['job_id'],'hope_language_id','job_hope_lang_inner', $lang_data);
			$job['Mem'] = $this->getSelectByJobId($job['job_id'],'hope_member_id','job_hope_member_inner', $memb_type);
			$job['Att'] = $this->getSelectByJobId($job['job_id'],'attention_id','job_attention_inner', $attention_data);
			$job['Cname'] = $this->getSelectByJobId($job['job_id'],'category_id','job_category_inner', $temas);
			$d = array();
			$characters = array();
			foreach($job['Mem'] as $k=>$v)
			{
				if(isset($this->images_options['Mem_'.$k]))
				{
					$op = $this->images_options['Mem_'.$k];
					$characters[$op['sort']] = array('img'=>$op['img'],'css'=>$op['css'],'name'=>$v);
				}
			}
				
			foreach($job['Att'] as $k=>$v)
			{
				if(isset($this->images_options['Att_'.$k]))
				{
					$op = $this->images_options['Att_'.$k];
					$characters[$op['sort']] = array('img'=>$op['img'],'css'=>$op['css'],'name'=>$v);
				}
			}
				
			foreach($job['Lan'] as $k=>$v)
			{
				if(isset($this->images_options['Lan_'.$k]))
				{
					$op = $this->images_options['Lan_'.$k];
					$characters[$op['sort']] = array('img'=>$op['img'],'css'=>$op['css'],'name'=>$v);
				}
			}
			ksort($characters) ;
			$job['characters'] =array_slice($characters,0,4);
			$jobList[] = $job;
		}
		return $jobList;
	}
	
	function getCompanyNameById($id)
	{
		$sql = 'select company_name from Company_info where id='.$id;
		$p = $this->query($sql);
		$company_name = isset($p[0][0]['company_name'])?trim($p[0][0]['company_name']):"";
		return $company_name;
	}
	
	function getJobByCompanyID($company_id,$pagesize=3)
	{
		$fileds = ' Job.title,Job.work_type,Job.content,Job.id as job_id,Job.attention_img_file,Job.pickup_img_file';
		$select = "select ".$fileds." from job_mst as Job ";
		$where .="where Job.deleted is null and Job.search_flg=0 and Job.job_status_id  = 1 and (Job.public_start||' '||Job.public_start_hour||':'||Job.public_start_min) <= to_char(now(),'YYYY-MM-DD HH24:MI')";
		$where .="and (Job.public_end||' '||Job.public_end_hour||':'||Job.public_end_min)>=to_char(now(),'YYYY-MM-DD HH24:MI') ";
		$where .=" and (Job.public_close_date is null) and (Job.cn_asia<>'1' or Job.cn_asia is null)";

		$order = " order by Job.public_start desc,Job.public_start_hour desc,Job.public_start_min desc,Job.approve_date desc";
		$limit = " limit ".$pagesize;
		$sql = $select.$where.$order.$limit;

		$data = $this->query($sql);
		$jobList = array();
	
		foreach($data as $k=>$v)
		{
			$job = $v[0];
			
			$jobList[] = $job;
		}
		return $jobList;
	}
	
	function getSelectByJobId($id,$select,$tablename,$sourcedata) {
		$sql = "SELECT $select from $tablename  WHERE deleted is null and job_id=".$id;
		$data = $this->query($sql);
		if (!empty ($data)) {
			$arr = $arrName = array(); 
			foreach ($data as $d) {
				$lid = $d[0][$select];
 				$name = $sourcedata[$lid]?$sourcedata[$lid]:"";
				$arr[$lid] = $name;
			}
// 			$str = implode("、", $arr);
			return $arr;
		} else {
			return ;
		}
	}
	
	
	function getJobListByAreaId($areaid=null,$pagesize=5)
	{
		$date = date('Y-m-d');
		$datetime = date('Y-m-d H:i:s');
		$fileds = ' Job.title,Job.small_title,cinfo.company_name,Job.work_type,Job.content,Job.id as job_id,Job.location,Job.pickup_img_file,Job.attention_img_file';
		$select = "select ".$fileds." from job_mst as   Job  join company_info as  cinfo on (cinfo.id=Job.company_id and cinfo.deleted is null)";
		$where =" WHERE	Job.deleted IS NULL AND Job.search_flg = 0 AND (Job.public_close_date IS NULL) AND (Job.cn_asia <> '1' OR Job.cn_asia IS NULL)";
		$where .=" AND (((  Job.attention_img_file != '' AND Job.attention_search_flg = 1 and job_status_id =1  and Job.attention_start <= '".$date."' AND Job.attention_end >= '".$date."')"; 
		$where .=" or (Job.pickup_img_file!='' and Job.pickup_search_flg = 1 and Job.pickup_start<='".$date."' and Job.pickup_end>='".$date."')";
		$where .=" or (Job.public_start is not null and Job.attention_start is null and Job.pickup_start is null)) AND 	(Job.public_start || ' ' || Job.public_start_hour || ':' || Job.public_start_min<= '".$datetime."'";
		$where .=" AND 	Job.public_end || ' ' || Job.public_end_hour || ':' || Job.public_end_min>= '".$datetime."'))";
		
		if($areaid)
		{
			$join .=" join job_area_inner as ainner on (ainner.job_id = Job.id and ainner.deleted is null) ";
			$where .=" and job_area_id=".$areaid;
		}
		$order = " order by attention_start is not null desc,attention_start desc , pickup_start is not null desc ,pickup_start desc,public_start desc ,Job.approve_date desc";
		$limit = ' offset 0 limit '.$pagesize;
		$sql = $select.$join.$where.$order.$limit;
		$data = $this->query($sql);
		$jobList = array();
		foreach($data as $k=>$v)
		{
			$job = $v[0];
			if($job['attention_img_file'])
				$job['job_img_file'] = '/upload/image/job/'.floor($job['job_id']/1000).'/'.$job['job_id'].'/'.$job['attention_img_file'];
			else if($job['pickup_img_file'])
				$job['job_img_file'] = '/upload/image/job/'.floor($job['job_id']/1000).'/'.$job['job_id'].'/'.$job['pickup_img_file'];
			else if($job['img_file'])
				$job['job_img_file'] = '/upload/image/job/'.floor($job['job_id']/1000).'/'.$job['job_id'].'/'.$job['img_file'];
			$jobList[] = $job;
		}
		return $jobList;
	}
	
	function getMobilePickUpJob($pagesize=5,$limit = 3)
	{
		$date = date('Y-m-d');
		$datetime = date('Y-m-d H:i:s');
		$fileds = ' Job.title,cinfo.company_name,Job.work_type,Job.content,Job.id as job_id,Job.location,Job.pickup_img_file,Job.attention_img_file,Job.img_file,small_title';
		$select = "select ".$fileds." from job_mst as   Job  join company_info as  cinfo on (cinfo.id=Job.company_id and cinfo.deleted is null)";
		$where =" WHERE	Job.deleted IS NULL AND Job.search_flg = 0 AND (Job.public_close_date IS NULL) AND (Job.cn_asia <> '1' OR Job.cn_asia IS NULL)";
		$where .=" AND (((  Job.attention_img_file != '' AND Job.attention_search_flg = 1 and job_status_id =1 and Job.attention_start <= '".$date."' AND Job.attention_end >= '".$date."')"; 
		$where .=" or (Job.pickup_img_file!='' and Job.pickup_search_flg = 1 and Job.pickup_start<='".$date."' and Job.pickup_end>='".$date."')";
		$where .=" or (Job.public_start is not null and Job.attention_start is null and Job.pickup_start is null)) AND 	(Job.public_start || ' ' || Job.public_start_hour || ':' || Job.public_start_min<= '".$datetime."'";
		$where .=" AND 	Job.public_end || ' ' || Job.public_end_hour || ':' || Job.public_end_min>= '".$datetime."'))";
	
		$order = "order by attention_start is not null desc,attention_start desc , pickup_start is not null desc ,pickup_start desc,public_start desc ,Job.approve_date desc";
		$limit = ' offset 0 limit '.$pagesize;
		$sql = $select.$where.$order.$limit;
		$data = $this->query($sql);
		$jobList = array();
		foreach($data as $k=>$v)
		{
			$job = $v[0];
			if($job['attention_img_file'])
				$job['job_img_file'] = '/upload/image/job/'.floor($job['job_id']/1000).'/'.$job['job_id'].'/'.$job['attention_img_file'];
			else if($job['pickup_img_file'])
				$job['job_img_file'] = '/upload/image/job/'.floor($job['job_id']/1000).'/'.$job['job_id'].'/'.$job['pickup_img_file'];
			else if($job['img_file'])
				$job['job_img_file'] = '/upload/image/job/'.floor($job['job_id']/1000).'/'.$job['job_id'].'/'.$job['img_file'];
			$jobList[] = $job;
		}
		return $jobList;
	}
	
	function getMobileCategoryList()
	{
		$category_config_list = array_splice(parse_ini_file("../views/category/config.ini", true),1,1);
		$category_keys = explode(',',trim($category_config_list['category_item']['keys']));
		$category_vals = explode(',',trim($category_config_list['category_item']['values']));
		if (!empty($category_keys) && !empty($category_vals)) {
			$category_list = array_combine($category_keys, $category_vals);
		}
		return $category_list;
	}
}
?>
